<template>
  <div class="register">
      <div class="toDay"><span class="back"></span>{{title}}</div>
      <div class="info">
        <p class="title"><input type="text" v-model="date.mobile" placeholder="手机号码" name=""></p>
        <p class="title"><input type="text" v-model="date.password" placeholder="密码" name=""></p>
        <p class="title"><input type="text" v-model="date.nickname" placeholder="用户名称" name=""></p>
        <p class="title">
          <select class="select" name="" v-model="date.gender" >
            <option value="1">男</option>
            <option value="2">女</option>
          </select>
        </p>
        <p class="txtle">
          <button @click="register">注册</button>
          <button @click="goLogin">登录</button>
        </p>
      </div>
  </div>
</template>

<script>
import {Get,Post,Put,Del,Reg,Login} from '@/config/api.js'


export default {
  name: 'register',
  data () {
    return {
      title:'用户注册',
      date:{
        mobile:'',
        password:'',
        nickname:'',
        gender:1,
      }
    }
  },

  mounted(){

  },
  methods:{
    goLogin(){
      this.$router.push('/login')
    },
    //点击注册
    register(){
        //发送注册请求
        let mobile=this.date.mobile
        let password=this.date.password
        let nickname=this.date.nickname
        let gender=this.date.gender

        if(mobile!=''&&password!=''&&nickname!=''){
            let date={
                "registered_method": "mobile",
                "mobile": mobile,
                "password": password,
                "nickname": nickname,
                "gender": gender
            };
            let that=this
            Reg('/users',date,this.$store.state.token).then(function(reg){
              alert('注册成功!')
              that.clickLogin(mobile,password)
            }).catch(function(err){
              alert(err.response.data.message)
            })

            
        }else{
          alert('请输入相关信息！')
        }
     
    },
        //点击登录
    async clickLogin(name,password){
      console.log(333)
      if(name!=''&&password!=''){

        let date={
            "grant_type": "password",
            "client_id": "game-ios",
            "client_secret": "0a0e2b7c0f223ff235810cbdc70f1d98",
            "scope": "game_ios",
            "username": name,
            "password": password
        };
        let that=this;
        let reg=await Login(date)
        let token=reg.data.access_token
        let arr = token.split('');
        let passToken = "";
        for(let i=0;i<token.length;i++){
          passToken += arr[i].charCodeAt()*3;
          passToken += "/";
        }
        let lastToken=passToken.substring(0,passToken.length-1)
        let d = new Date();
        d.setTime(d.getTime() + (604800000));
        let expires = "expires=" + d.toUTCString();
        document.cookie = "bedLogin" + "=" + lastToken + "; " + expires;
        document.cookie = "bedtoken" + "=" + lastToken + "; " + expires;
        that.$store.dispatch("loginsync");
        this.$router.push('/about')
      }
      
    }

  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
